package com.nd.android.u.ui.widge.lift;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class PetalView extends SurfaceView implements SurfaceHolder.Callback {

	protected static int DIE_OUT_LINE=80;     //鲜花或者蛋糕的初始位置
	protected PetalSet mPetalSet;
	protected PetalThread mPetalThread;
	private DrawThread mDrawThread;
	private boolean isSelection;    //selection   对送蛋糕还是送鲜花的选择。    0  是蛋糕      1是鲜花
	private Paint mPaint = new Paint();
	private Petal mDrawPetal;
	private ArrayList<Petal> mPetalList;


	public PetalView(Context context, int wth, int height,boolean selection) {
		super(context);
		getHolder().addCallback(this);
		setZOrderOnTop(true);                                 //设动画背景色
		getHolder().setFormat(PixelFormat.TRANSLUCENT);        //为透明色
		DIE_OUT_LINE += height;     //用来判断一个鲜花粒子或者蛋糕粒子移除的条件
		mDrawThread = new DrawThread(this, getHolder());
		isSelection=selection;
		mPetalSet = new PetalSet(isSelection);
		mPetalThread = new PetalThread(this, getResources(), wth);

	}
	
	/**
	 * 对某一时刻动画的绘制
	 */
	public void doDraw(Canvas canvas) {
		canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
        
		mPetalList = mPetalSet.petalList;

		for (int i = 0; i < mPetalList.size(); i++) {
			mDrawPetal = mPetalList.get(i);
			int tempX = mDrawPetal.getX();
			int tempY = mDrawPetal.getY();

			canvas.drawBitmap(mDrawPetal.getImage(), tempX, tempY, mPaint);       
		}

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {

	 if(mPetalThread!=null&&(mDrawThread!=null)){	
		if (!mPetalThread.isAlive()) {    //判断线程是否运动，若没有，重新启动
			mPetalThread.start();
		}
		if (!mDrawThread.isAlive()) {
			mDrawThread.start();
		}
	 }
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		if(mDrawThread!=null&&(mPetalThread!=null)){
		 mDrawThread.isDrawFlag = false;                    //当surface破坏时，置DrawThread为空，不执行绘制
		 mDrawThread = null;
		 mPetalThread.isPetalFlag = false;
		 mPetalThread = null;
		}
	}
	
	public static class DrawThread extends Thread {
		private PetalView mPetalView;
		protected boolean isDrawFlag; // 画布是否可以绘制的标志־
		private SurfaceHolder mSurfaceHolder;
		private final int SLEEPTIME = 4; //每次线程绘制完的睡眠时间
		private Canvas canvas = null;

		
		public DrawThread(PetalView pv, SurfaceHolder sHolder) {
			this.mSurfaceHolder = sHolder;
			this.isDrawFlag = true;
			this.mPetalView = pv;
		}
		
		/**
		 * 对动画的绘制
		 */
		public void run() {

			while (isDrawFlag) {                                           
				try {
					canvas = mSurfaceHolder.lockCanvas(null);

					synchronized (mSurfaceHolder) {
						mPetalView.doDraw(canvas);
					}
				} catch (Exception e) {
					e.printStackTrace(); // 捕获并打印异常
				} finally {
					if (canvas != null) {
						mSurfaceHolder.unlockCanvasAndPost(canvas);//surfaceHolder解锁并将画布对象传回
					}
				}

				try {
					Thread.sleep(SLEEPTIME);//线程休眠一段时间
				} catch (Exception e) {
					e.printStackTrace();//  捕获并打印异常
				}
			}
		}

	}
	
	public static class PetalThread extends Thread {

		protected boolean isPetalFlag;                     //产生花瓣的线程是否开始的标志
		protected int num =7;                      //用于计数，来判断花瓣产生的频率־
		private final static int SLEEPTIME = 21;        //花瓣每次产生过程后的睡眠时间   
		private double time = 0;                
		private final static int SPAN = 1;           
		private PetalView petalView;
		private Resources mResources;
		private int width;                        //展示动画的窗口宽度	
		private Petal mPetal;

		public PetalThread(PetalView pv, Resources res, int wth) {
			this.petalView = pv;
			this.isPetalFlag = true;
			this.mResources = res;
			width = wth;

		}
		
		/**
		 * 产生鲜花掉落过程中的坐标
		 */
		public void run() {
			while (isPetalFlag) {

				if (time % num == 0) {

					petalView.mPetalSet.add(1, time, mResources, width);
				}
				ArrayList<Petal> petalList = petalView.mPetalSet.petalList;

				for (int i = 0; i < petalList.size(); i++) {   //鲜花某一时刻坐标
					mPetal = petalList.get(i);
					double timeSpan = (time - mPetal.getStartTime()) / 4;
					int tempX = (int) (mPetal.getStartX() + mPetal.getHorizontal_v() * timeSpan);
					int tempY = (int) (mPetal.getStartY() + mPetal.getVertical_v() * timeSpan);
					if (tempY > PetalView.DIE_OUT_LINE) {
						petalList.remove(i);
					}
					mPetal.setX(tempX);
					mPetal.setY(tempY);
				}

				time +=SPAN;

				try {
					Thread.sleep(SLEEPTIME);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}
	}

}
